<template>
  <div class="profile-container">
    <div class="profile-header">
      <div class="profile-cover">
        <img src="/covers/default-cover.jpg" alt="封面图">
        <button class="edit-cover-btn" @click="editCover">
          <i class="fas fa-camera"></i>
        </button>
      </div>
      <div class="profile-info">
        <div class="avatar-section">
          <img :src="userInfo.image || '/avatars/default-avatar.jpg'" :alt="userInfo.username" class="avatar">
          <button class="edit-avatar-btn" @click="editAvatar">
            <i class="fas fa-camera"></i>
          </button>
        </div>
        <div class="user-info">
          <h1>{{ userInfo.username }}</h1>
          <div class="user-level">
            <span class="level-badge">游戏达人</span>
            <div class="exp-bar">
              <div class="exp-progress" style="width: 75%"></div>
            </div>
          </div>
          <p class="user-bio">{{ userInfo.signature || '热爱游戏，热爱分享。专注于角色培养和副本攻略。' }}</p>
        </div>
        <div class="profile-actions">
          <button class="edit-profile-btn" @click="editProfile">
            <i class="fas fa-edit"></i>
            编辑资料
          </button>
        </div>
      </div>
    </div>

    <div class="profile-content">
      <div class="profile-sidebar">
        <div class="sidebar-section">
          <h3>账号信息</h3>
          <ul class="info-list">
            <li>
              <span class="info-label">UID</span>
              <span class="info-value">{{ userInfo.id }}</span>
            </li>
            <li>
              <span class="info-label">注册时间</span>
              <span class="info-value">{{ formatDate(userInfo.createTime) }}</span>
            </li>
            <li>
              <span class="info-label">最后登录</span>
              <span class="info-value">{{ formatDate(userInfo.updateTime) }}</span>
            </li>
          </ul>
        </div>

        <div class="sidebar-section">
          <h3>联系方式</h3>
          <ul class="info-list">
            <li>
              <span class="info-label">邮箱</span>
              <span class="info-value">{{ userInfo.email || '未设置' }}</span>
            </li>
            <li>
              <span class="info-label">手机号</span>
              <span class="info-value">{{ userInfo.phone || '未设置' }}</span>
            </li>
            <li>
              <span class="info-label">性别</span>
              <span class="info-value">{{ userInfo.sex || '未设置' }}</span>
            </li>
          </ul>
        </div>
      </div>

      <div class="profile-main">
        <div class="content-tabs">
          <button 
            v-for="tab in tabs" 
            :key="tab.id"
            :class="{ active: currentTab === tab.id }"
            @click="currentTab = tab.id"
          >
            {{ tab.name }}
          </button>
        </div>

        <div class="tab-content">
          <div v-if="currentTab === 'posts'" class="posts-list">
            <div v-if="myPosts.length === 0" class="empty-tip">暂无帖子</div>
            <div v-else>
              <div class="post-list">
                <div v-for="post in myPosts" :key="post.id" class="post-card" @click="goToBlogDetail(post.id)">
                  <div class="post-avatar">
                    <img :src="post.cover || '/avatars/default-avatar.jpg'" alt="封面" style="width:48px;height:48px;border-radius:50%">
                  </div>
                  <div class="post-content">
                    <h3 class="post-title">{{ post.title }}</h3>
                    <p class="post-excerpt">{{ post.descr }}</p>
                    <div class="post-meta">
                      <span class="post-time">{{ post.createTime }}</span>
                      <span class="post-stats"><i class="fas fa-eye"></i> {{ post.readCount }}</span>
                    </div>
                  </div>
                </div>
              </div>
              <div class="pagination">
                <el-select v-model="myPostsPageSize" @change="handleMyPostsSizeChange" size="small" style="width: 100px; margin-right: 1rem;">
                  <el-option v-for="n in 10" :key="n" :label="`${n} 条/页`" :value="n" />
                </el-select>
                <button :disabled="myPostsPage === 1" @click="prevMyPostsPage">上一页</button>
                <span class="page-info">{{ myPostsPage }} / {{ myPostsTotalPages }}</span>
                <button :disabled="myPostsPage === myPostsTotalPages" @click="nextMyPostsPage">下一页</button>
              </div>
            </div>
          </div>

          <div v-if="currentTab === 'comments'" class="comments-list">
            <div class="empty-tip">暂无评论</div>
          </div>

          <div v-if="currentTab === 'collections'" class="collections-list">
            <div class="empty-tip">暂无收藏</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, computed, watch } from 'vue'
import request from '@/utils/request'
import { ElMessage } from 'element-plus'
import { useRouter, useRoute } from 'vue-router'

interface UserInfo {
  id: number
  username: string
  email: string
  phone: string
  createTime: string
  updateTime: string
  sex: string
  image: string
  signature: string
  status: number
}

const currentTab = ref('posts')
const tabs = [
  { id: 'posts', name: '我的帖子' },
  { id: 'comments', name: '我的评论' },
  { id: 'collections', name: '我的收藏' }
]

const userInfo = ref<UserInfo>({
  id: 0,
  username: '',
  email: '',
  phone: '',
  createTime: '',
  updateTime: '',
  sex: '',
  image: '',
  signature: '',
  status: 0
})

const myPosts = ref<any[]>([])
const myPostsTotal = ref(0)
const myPostsPage = ref(1)
const myPostsPageSize = ref(5)
const myPostsTotalPages = computed(() => Math.max(1, Math.ceil(myPostsTotal.value / myPostsPageSize.value)))

const router = useRouter()
const route = useRoute()
const blogId = route.params.id
console.log('当前博客id:', blogId)

// 格式化日期
const formatDate = (dateStr: string) => {
  if (!dateStr) return '未知'
  const date = new Date(dateStr)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 获取用户信息
const getUserInfo = async () => {
  try {
    const response = await request.get('/user/user/detail')
    userInfo.value = response.data
  } catch (error) {
    console.error('获取用户信息失败:', error)
    ElMessage.error('获取用户信息失败')
  }
}

const editCover = () => {
  // TODO: 实现封面图编辑
}

const editAvatar = () => {
  // TODO: 实现头像编辑
}

const editProfile = () => {
  window.location.href = 'http://localhost:8181/settings'
}

const getMyPosts = async () => {
  try {
    const params = {
      createUser: userInfo.value.id,
      page: myPostsPage.value,
      pageSize: myPostsPageSize.value
    }
    const response = await request.get('/user/blog/byuserid', { params })
    const pageResult = response.data
    myPosts.value = pageResult.records
    myPostsTotal.value = pageResult.total
  } catch (error) {
    ElMessage.error('获取我的帖子失败')
  }
}

const handleMyPostsSizeChange = (size: number) => {
  myPostsPageSize.value = size
  myPostsPage.value = 1
  getMyPosts()
}
const prevMyPostsPage = () => {
  if (myPostsPage.value > 1) {
    myPostsPage.value--
    getMyPosts()
  }
}
const nextMyPostsPage = () => {
  if (myPostsPage.value < myPostsTotalPages.value) {
    myPostsPage.value++
    getMyPosts()
  }
}

const goToBlogDetail = (id: number) => {
  router.push(`/forum/blog/${id}`)
}

watch([myPostsPage, myPostsPageSize, currentTab], ([page, size, tab]) => {
  if (tab === 'posts') {
    getMyPosts()
  }
})

onMounted(() => {
  getUserInfo()
  if (currentTab.value === 'posts') {
    getMyPosts()
  }
})
</script>

<style scoped>
.profile-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.profile-header {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 2rem;
}

.profile-cover {
  position: relative;
  height: 200px;
  overflow: hidden;
}

.profile-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.edit-cover-btn {
  position: absolute;
  bottom: 1rem;
  right: 1rem;
  padding: 0.5rem;
  background: rgba(0, 0, 0, 0.5);
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.profile-info {
  padding: 2rem;
  display: flex;
  gap: 2rem;
  position: relative;
}

.avatar-section {
  position: relative;
  margin-top: -60px;
}

.avatar {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  border: 4px solid white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.edit-avatar-btn {
  position: absolute;
  bottom: 0;
  right: 0;
  padding: 0.5rem;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 50%;
  cursor: pointer;
}

.user-info {
  flex: 1;
}

.user-info h1 {
  margin: 0 0 1rem;
  color: #2c3e50;
}

.user-level {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
}

.level-badge {
  padding: 0.2rem 0.5rem;
  background-color: #42b983;
  color: white;
  border-radius: 4px;
  font-size: 0.9rem;
}

.exp-bar {
  flex: 1;
  height: 8px;
  background-color: #f5f5f5;
  border-radius: 4px;
  overflow: hidden;
}

.exp-progress {
  height: 100%;
  background-color: #42b983;
  transition: width 0.3s;
}

.user-bio {
  color: #666;
  margin: 0;
}

.profile-actions {
  position: absolute;
  top: 2rem;
  right: 2rem;
}

.edit-profile-btn {
  padding: 0.5rem 1rem;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.profile-content {
  display: flex;
  gap: 2rem;
}

.profile-sidebar {
  width: 300px;
  flex-shrink: 0;
}

.sidebar-section {
  background: white;
  border-radius: 8px;
  padding: 1.5rem;
  margin-bottom: 1rem;
}

.sidebar-section h3 {
  margin: 0 0 1rem;
  color: #2c3e50;
}

.info-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.info-list li {
  display: flex;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}

.info-label {
  color: #666;
}

.info-value {
  color: #2c3e50;
  font-weight: 500;
}

.profile-main {
  flex: 1;
  background: white;
  border-radius: 8px;
  padding: 1.5rem;
}

.content-tabs {
  display: flex;
  gap: 1rem;
  margin-bottom: 1.5rem;
  border-bottom: 1px solid #eee;
  padding-bottom: 1rem;
}

.content-tabs button {
  padding: 0.5rem 1rem;
  border: none;
  background: none;
  color: #666;
  cursor: pointer;
  position: relative;
}

.content-tabs button.active {
  color: #42b983;
}

.content-tabs button.active::after {
  content: '';
  position: absolute;
  bottom: -1rem;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #42b983;
}

.empty-tip {
  text-align: center;
  color: #999;
  padding: 2rem;
}

.post-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.post-card {
  background: #f9f9f9;
  border-radius: 8px;
  padding: 1rem;
  display: flex;
  gap: 1rem;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}
.post-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}
.post-avatar img {
  width: 48px;
  height: 48px;
  border-radius: 50%;
}
.post-content {
  flex: 1;
}
.post-title {
  margin: 0 0 0.5rem 0;
  font-size: 1.1rem;
}
.post-excerpt {
  color: #666;
  margin: 0.5rem 0;
  font-size: 0.9rem;
}
.post-meta {
  display: flex;
  gap: 1rem;
  font-size: 0.9rem;
  color: #999;
}
.post-meta i {
  margin-right: 0.3rem;
}
.pagination {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  gap: 1rem;
  margin-top: 1rem;
}
.pagination button {
  padding: 0.5rem 1rem;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
}
.pagination button:disabled {
  background-color: #f5f5f5;
  cursor: not-allowed;
}
.page-info {
  color: #666;
}

.comment-section { margin-top: 2rem; background: #fff; border-radius: 8px; padding: 1.5rem; }
.comment-item { border-bottom: 1px solid #eee; padding: 1rem 0; }
.comment-main { margin-bottom: 0.5rem; }
.comment-replies { margin-left: 2rem; }
.comment-reply { background: #f9f9f9; border-radius: 4px; padding: 0.5rem; margin-bottom: 0.5rem; }
.add-comment, .reply-box { margin-top: 1rem; }
.empty-tip { color: #999; text-align: center; }
</style> 